home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Garbo
/
Garbo.cdr
/
mac
/
source
/
music4c.sit
/
Music4C Folder
/
SFConvert folder
/
sdTo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-06-26
|
10KB
|
450 lines
#include "SFConvert.h"
#include <stdio.h>
#include <unix.h>
#include <string.h>
#include <math.h>
#include <SANE.h>
#include "SDtype.h"
extern long TotalSamps;
extern CursHandle watchCurs;
extern int SoundFileType;
extern long SampleRate;
extern long NumChannels;
extern double MaxSample;
extern double MinSample;
extern long fileSize;
extern ioParam myIOParmBlk;
extern ioParam NewParmBlk;
extern int SFOUTPUTtype;
extern Boolean SDnoResource;
extern OSErr theErr;
extern Str255 theMess;
extern int nrec;
extern long RecLength;
Boolean SD1ToAIFF(void);
Boolean SD2ToAIFF(void);
Boolean SD2ToFloat(void);
Boolean SD1ToFloat(void);
Boolean SD2ToFloat(void);
Boolean SD2ToCHUNKY(void);
Boolean SD1ToCHUNKY(void);
Boolean SD2ToINT16(void);
Boolean SD1ToINT16(void);
extern void OSError(Str255, Str255);
extern void DoOSErrorAlert(Str255, Str255);
Boolean SD1ToAIFF()
{
return(FALSE);
}
Boolean SD2ToAIFF()
{
return(FALSE);
}
Boolean SD2ToFloat()
{
register long i;
register double x;
register int ix;
double peak;
Str255 mess;
int *theIbuf, *Iptr;
float *sp, *SampBuf;
long nBytes;
long nSamps;
long bytesLeft;
long sampBufsz;
myIOParmBlk.ioCompletion = NIL;
PBGetEOF(&myIOParmBlk, FALSE);
if ( (fileSize = (long)myIOParmBlk.ioMisc) <= 0 ) {
OSError("\pfile is empty!", NIL);
return(FALSE);
}
TotalSamps = fileSize / sizeof(int);
/* SampleRate = Header.SampRate;*/
/* get sample raste from resource */
nrec = 0;
SetProgressDialog();
sampBufsz = RecLength / sizeof(int);
theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
if ( (theErr = MemError()) != noErr ) {
DoOSErrorAlert("\pcan't get enough memory for theIbuf", NIL);
return(FALSE);
}
SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
if ( (theErr = MemError()) != noErr ) {
DoOSErrorAlert("\pcan't get enough memory for SampBuf", NIL);
return(FALSE);
}
MinSample = 0.0;
MaxSample = 0.0;
bytesLeft = fileSize;
myIOParmBlk.ioPosMode = fsAtMark;
myIOParmBlk.ioPosOffset = NIL;
while ( bytesLeft > 0L ) {
if ( bytesLeft > RecLength )
nBytes = RecLength;
else
nBytes = bytesLeft;
myIOParmBlk.ioBuffer = (Ptr)theIbuf;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file", NIL);
return(FALSE);
}
nBytes = myIOParmBlk.ioActCount;
if ( nBytes != myIOParmBlk.ioReqCount )
DoOSErrorAlert("\pRead less bytes than expected", NIL);
nSamps = nBytes / sizeof(int);
for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
ix = *Iptr++;
x = (double)(ix);
if ( x < MinSample )
MinSample = x;
if ( x > MaxSample )
MaxSample = x;
*sp++ = x;
}
/* write it out */
NewParmBlk.ioReqCount = (long)(nSamps * sizeof(float));
NewParmBlk.ioBuffer = (Ptr)SampBuf;
if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
DoOSErrorAlert("\pError writing to sample file", NIL);
}
if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(float)) ) {
DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
}
nrec++;
if ( !UpdateProgressDialog() ) {
DisposPtr((Ptr)SampBuf);
DisposPtr((Ptr)theIbuf);
DisposeProgDialog();
InitCursor();
return(FALSE);
}
bytesLeft -= nBytes;
}
DisposPtr((Ptr)SampBuf);
DisposPtr((Ptr)theIbuf);
DisposeProgDialog();
InitCursor();
return(TRUE);
}
Boolean SD1ToFloat()
{
register long i;
register double x;
register int ix;
double peak;
Str255 mess;
int *theIbuf, *Iptr;
float *sp, *SampBuf;
long nBytes;
long nSamps;
long bytesLeft;
long sampBufsz;
WaveRec Header;
WavPtr HeaderPtr;
RecLength = (long)(16384);
/* read the header */
HeaderPtr = &Header;
nBytes = sizeof(Header.HeaderSize);
myIOParmBlk.ioPosMode = fsFromStart;
myIOParmBlk.ioPosOffset = 0L;
myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file reached Header", NIL);
else {
sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
DoOSErrorAlert(theMess, NIL);
return(FALSE);
}
return(FALSE);
}
if ( myIOParmBlk.ioActCount != nBytes ) {
DoOSErrorAlert("\perror reading HeaderSize", NIL);
}
nBytes = Header.HeaderSize;
myIOParmBlk.ioPosMode = fsFromStart;
myIOParmBlk.ioPosOffset = 0L;
myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file reached Header", NIL);
else {
sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
DoOSErrorAlert(theMess, NIL);
}
return(FALSE);
}
fileSize = Header.FileSize;
TotalSamps = fileSize / sizeof(int);
SampleRate = Header.SampRate;
nrec = 0;
SetProgressDialog();
sampBufsz = RecLength / sizeof(int);
theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
if ( (theErr = MemError()) != noErr ) {
DoOSErrorAlert("\pcan't get enough memory for theIbuf", NIL);
return(FALSE);
}
SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
if ( (theErr = MemError()) != noErr ) {
DoOSErrorAlert("\pcan't get enough memory for SampBuf", NIL);
return(FALSE);
}
MinSample = 0.0;
MaxSample = 0.0;
bytesLeft = fileSize;
myIOParmBlk.ioPosMode = fsAtMark;
myIOParmBlk.ioPosOffset = NIL;
while ( bytesLeft > 0L ) {
if ( bytesLeft > RecLength )
nBytes = RecLength;
else
nBytes = bytesLeft;
myIOParmBlk.ioBuffer = (Ptr)theIbuf;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file", NIL);
return(FALSE);
}
nBytes = myIOParmBlk.ioActCount;
if ( nBytes != myIOParmBlk.ioReqCount )
DoOSErrorAlert("\pRead less bytes than expected", NIL);
nSamps = nBytes / sizeof(int);
for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
ix = *Iptr++;
x = (double)(ix);
if ( x < MinSample )
MinSample = x;
if ( x > MaxSample )
MaxSample = x;
*sp++ = x;
}
/* write it out */
NewParmBlk.ioReqCount = (long)(nSamps * sizeof(float));
NewParmBlk.ioBuffer = (Ptr)SampBuf;
if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
DoOSErrorAlert("\pError writing to sample file", NIL);
}
if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(float)) ) {
DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
}
nrec++;
if ( !UpdateProgressDialog() ) {
DisposPtr((Ptr)SampBuf);
DisposPtr((Ptr)theIbuf);
DisposeProgDialog();
InitCursor();
return(FALSE);
}
bytesLeft -= nBytes;
}
DisposPtr((Ptr)SampBuf);
DisposPtr((Ptr)theIbuf);
DisposeProgDialog();
InitCursor();
return(TRUE);
}
Boolean SD2ToCHUNKY()
{
return(FALSE);
}
Boolean SD1ToCHUNKY()
{
}
Boolean SD2ToINT16()
{
return(FALSE);
}
Boolean SD1ToINT16()
{
register long i;
register double scalefactor;
double peak;
Str255 mess;
int *theIbuf, *Iptr;
long nBytes;
long nSamps;
long bytesLeft;
long sampBufsz;
WaveRec Header;
WavPtr HeaderPtr;
RecLength = (long)(16384);
HeaderPtr = &Header;
/* read the header */
nBytes = sizeof(Header.HeaderSize);
myIOParmBlk.ioPosMode = fsFromStart;
myIOParmBlk.ioPosOffset = 0L;
myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file reached Header", NIL);
else {
sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
DoOSErrorAlert(theMess, NIL);
}
return(FALSE);
}
if ( myIOParmBlk.ioActCount != nBytes ) {
DoOSErrorAlert("\perror reading HeaderSize", NIL);
}
nBytes = Header.HeaderSize;
myIOParmBlk.ioPosMode = fsFromStart;
myIOParmBlk.ioPosOffset = 0L;
myIOParmBlk.ioBuffer = (Ptr)HeaderPtr;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file reached Header", NIL);
else {
sprintf((char *)theMess, "\pError reading Header, theErr = %d\n", theErr);
DoOSErrorAlert(theMess, NIL);
return(FALSE);
}
return(FALSE);
}
fileSize = Header.FileSize;
TotalSamps = fileSize / sizeof(int);
SampleRate = Header.SampRate;
nrec = 0;
SetProgressDialog();
sampBufsz = RecLength / sizeof(int);
theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
if ( (theErr = MemError()) != noErr ) {
DoOSErrorAlert("\pcan't get enough memory for theIbuf", NIL);
return(FALSE);
}
if ( SDnoResource ) {
scalefactor = 1.0;
NumChannels = 1;
MinSample = 0.0;
MaxSample = 0.0;
}
else {
peak = MAX(fabs(MinSample), fabs(MaxSample));
scalefactor = (SAMPMAX / 2.0) / peak;
MinSample = 0.0;
MaxSample = 0.0;
}
bytesLeft = fileSize;
myIOParmBlk.ioPosMode = fsAtMark;
myIOParmBlk.ioPosOffset = NIL;
while ( bytesLeft > 0L ) {
if ( bytesLeft > RecLength )
nBytes = RecLength;
else
nBytes = bytesLeft;
myIOParmBlk.ioBuffer = (Ptr)theIbuf;
myIOParmBlk.ioReqCount = nBytes;
theErr = PBRead(&myIOParmBlk, FALSE);
if (theErr != noErr ) {
if ( theErr == eofErr )
DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
return(FALSE);
}
nBytes = myIOParmBlk.ioActCount;
if ( nBytes != myIOParmBlk.ioReqCount )
DoOSErrorAlert("\pRead less bytes than expected", NIL);
nSamps = nBytes / sizeof(int);
for ( i = 0, Iptr = theIbuf; i < nSamps; i++ ) {
*Iptr *= scalefactor;
if ( *Iptr < MinSample )
MinSample = *Iptr;
if ( *Iptr > MaxSample )
MaxSample = *Iptr;
*Iptr++;
}
/* write it out */
NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
NewParmBlk.ioBuffer = (Ptr)theIbuf;
if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) {
DoOSErrorAlert("\pError writing to sample file", NIL);
}
if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
}
nrec++;
if ( !UpdateProgressDialog() ) {
DisposPtr((Ptr)theIbuf);
DisposeProgDialog();
InitCursor();
return(FALSE);
}
bytesLeft -= nBytes;
}
DisposPtr((Ptr)theIbuf);
DisposeProgDialog();
InitCursor();
return(TRUE);
}